package com.fingard.util;

/**
 * Created by F on 2019/4/3.
 */
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GZIPUtils {

    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
    public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";

    public static byte[] compress(String str, String encoding) {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip =new GZIPOutputStream(out);
            gzip.write(str.getBytes(encoding));
            gzip.close();
        } catch ( Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }
    //返回base64编码
    public static String compressToString(String str, String encoding)throws Exception {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip =new GZIPOutputStream(out);
            gzip.write(str.getBytes(encoding));
            gzip.close();
        } catch ( Exception e) {
            e.printStackTrace();
        }
        return (new BASE64Encoder()).encode(out.toByteArray());
    }

    public static byte[] uncompress(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out =new ByteArrayOutputStream();
        ByteArrayInputStream in =new ByteArrayInputStream(bytes);
        try
        {
            GZIPInputStream ungzip =new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer,0, n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static String uncompressToString(byte[] bytes, String encoding) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            return out.toString(encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String uncompressToString(String zipDateStr, String encoding)throws Exception{
        byte[]zipDateByte = (new BASE64Decoder()).decodeBuffer(zipDateStr);
        return uncompressToString(zipDateByte,encoding);
    }

    public static void main(String[] args) throws Exception {
        String plantDate = "8246A4D3F35FB1725DF5D43F94F5889ADC2AE5CE186CF2A7EADC7969A3D89F04sssss121212122222222222222222222222222222222222222222aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaSoQ5ge0FJ82RPNriht2wCyFCAgPG1gLq5IhpBxyoMgc2asdadsdsssssssssssssssssssssssssssssdsdsaaaaadddaaaaaaaaaaaaaaaaadssddsdsd112ssssssssssssssssssssssssssssssssssss121212122222222222222222222222222222222222222222aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaSoQ5ge0FJ82RPNriht2wCyFCAgPG1gLq5IhpBxyoMgc2asdadsdsssssssssssssssssssssssssssssdsdsaaaaadddaaaaaaaaaaaaaaaaadssddsdsd112ssssssssssssssssssssssssssssssssssss121212122222222222222222222222222222222222222222aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaSoQ5ge0FJ82RPNriht2wCyFCAgPG1gLq5IhpBxyoMgc2asdadsdsssssssssssssssssssssssssssssdsdsaaaaadddaaaaaaaaaaaaaaaaadssddsdsd112ssssssssssssssssssssssssssssssssssss121212122222222222222222222222222222222222222222aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaSoQ5ge0FJ82RPNriht2wCyFCAgPG1gLq5IhpBxyoMgc2asdadsdsssssssssssssssssssssssssssssdsdsaaaaaddd";
        System.out.println("原文："+plantDate);
        System.out.println("字符串长度：" + plantDate.length());
        System.out.println("------------------------------------");
        String compress = compressToString(plantDate,"utf-8");

        System.out.println("压缩后：" + compress.length());
        System.out.println("压缩后字符串：" + compress);
        System.out.println("-------------------------------------");

        Map<String, Object> keyPairMap = RSAUtil.genKeyPair();
        String privateKey = RSAUtil.getPrivateKey(keyPairMap);
        String publicKey = RSAUtil.getPublicKey(keyPairMap);
        System.out.println("privateKey = [" + privateKey + "]");
        System.out.println("publicKey = [" + publicKey + "]");
        System.out.println("------------------------------------------------------------------");
        String encryptStr = RSAUtil.encryptByPublicKey(compress,"utf-8", publicKey);
        System.out.println("encryptStr = [" + encryptStr + "]");

        System.out.println("------------------------------------------------------------------");
        String decryptStr = RSAUtil.decryptByPrivateKey(encryptStr,"utf-8", privateKey);
        System.out.println("decryptStr = [" + decryptStr + "]");

        System.out.println("------------------------------------------------------------------");
        String uncompress = uncompressToString(compress,"utf-8");
        System.out.println("解压后：" + uncompress.length());
        System.out.println("解压后字符串：" +uncompress);
        System.out.println(plantDate.equals(uncompress));
    }
}
